/*******************************************************************************
 *
 * File Name:          TestMessageAction.java
 *
 * Created:            2007-09-03 18:47:06
 *
 * Original Author:    yu.peng
 *
 *------------------------------------------------------------------------------
 * MODIFICATION HISTORY
 *------------------------------------------------------------------------------
 * WHEN                WHO                 WHY
 * 2007-09-03 18:47:06 yu.peng             new
 *------------------------------------------------------------------------------
 *
 ******************************************************************************/
/*******************/
/* PACKAGE SECTION */
/*******************/

package org.njo.webapp.root.action.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.njo.webapp.root.model.activity.MessageResourcesActivity;
import org.njo.webapp.root.utility.DBMessageResources;

/**
 * TODO:comment
 *
 * @author yu.peng
 * @version 0.01
 */
public class TestMessageAction extends Action {

    // --------------------------------------------------------- Public Methods

    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping  The ActionMapping used to select this instance
     * @param form     The optional ActionForm bean for this request (if any)
     * @param request  The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @throws Exception if the application business logic throws
     *                   an exception
     */
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        MessageResourcesActivity messageResourcesActivity = new MessageResourcesActivity();
        
        String messageKey = request.getParameter("messageKey");
        String description = request.getParameter("description");

        String messageContent_default = request.getParameter("default");
        String messageContent_zh_CN = request.getParameter("zh_CN");
        String messageContent_en = request.getParameter("en");
        String messageContent_ja = request.getParameter("ja");
        
        Map<String, String> messageContents = new HashMap<String, String>();
        messageContents.put("default", messageContent_default);
        messageContents.put("zh_CN", messageContent_zh_CN);
        messageContents.put("en", messageContent_en);
        messageContents.put("ja", messageContent_ja);

        // TODO: check input

        messageResourcesActivity.saveMessage(messageKey, description, messageContents);
        
        MessageResources messageResources = this.getResources(request);
        if (messageResources instanceof DBMessageResources) {
            DBMessageResources dbMessageResources = (DBMessageResources)messageResources;
            dbMessageResources.reset();
        }
        
        return (new ActionForward("/testmessage.tiles?messageKey=" + messageKey, true));
    }

}
